class Solution {
    public int minTime(int n, int[][] edges, List<Boolean> hasApple) {
        List<Integer>[] graphs = new List[n];
         Arrays.setAll(graphs, i -> new ArrayList<>());
        for(int[] edge:edges)
        {
            int u = edge[0];
            int v = edge[1];
            graphs[u].add(v);
            graphs[v].add(u);
        }
        return Math.max(dfs(graphs,hasApple,0,-1)-2,0);
        
    }
    public int dfs(List<Integer>[] graphs,List<Boolean> hasApple,int node,int parent)
    {
        int res = 0;
        for(int var:graphs[node])
        {
            if(var!=parent)
            {
                res+=dfs(graphs,hasApple,var,node);
            }
        }
        return res+(res!=0||hasApple.get(node)?2:0);
    }
}